package com.carl.web.demo;

import com.alibaba.fastjson.JSON;
import com.carl.pojo.Brand;
import com.carl.service.BrandService;
import com.carl.service.UserService;
import com.carl.service.impl.BrandServiceImpl;

import javax.servlet.*;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;


@WebServlet("/selectAllServlet")
public class SelectAllServlet extends HttpServlet {
//    private UserService brandService =new UserService();
    private BrandService brandService = new BrandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGetSelect");
        response.setHeader(  "Access-Control-Allow-Origin","*");//允许所有来源访同
        response.setHeader(  "Access-Control-Allow-Method","POST,GET");//允许访问的方式
        //1. 调用Service查询
        List<Brand> brands = brandService.selectAll();

        //2. 将集合转换为JSON数据   序列化
        String jsonString = JSON.toJSONString(brands);

        //3. 响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
